package com.edu.exam.entity.assessment;

import com.edu.exam.enums.assessment.AssessmentTaskStatus;
import com.edu.exam.enums.common.TaskType;
import com.edu.exam.enums.common.ExamMode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 考核任务实体类
 * [所属模块] E-02 考核计划管理模块
 * 对应数据库表：assessment_tasks
 *
 * 用于管理各类考核任务的基本信息，包括任务编码、名称、类型、状态等。
 * 支持考核任务的创建、分配、执行和结果统计等全生命周期管理。
 * 关联课程、试卷、考试时间等关键信息，提供考核任务的完整视图。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class AssessmentPlanTask {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "任务编码不能为空")
    @Size(max = 50, message = "任务编码长度不能超过50个字符")
    @Column(name = "task_code", nullable = false, length = 50, unique = true)
    private String taskCode;

    @NotBlank(message = "任务名称不能为空")
    @Size(max = 200, message = "任务名称长度不能超过200个字符")
    @Column(name = "task_name", nullable = false, length = 200)
    private String taskName;

    @Size(max = 1000, message = "任务描述长度不能超过1000个字符")
    @Column(name = "task_description", length = 1000)
    private String taskDescription;

    @NotNull(message = "任务类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "task_type", nullable = false, length = 30)
    private TaskType taskType;

    @NotNull(message = "任务状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "task_status", nullable = false, length = 20)
    private AssessmentTaskStatus taskStatus;

    @NotNull(message = "课程ID不能为空")
    @Column(name = "course_id", nullable = false)
    private Long courseId;

    @Column(name = "course_code", length = 20)
    private String courseCode;

    @Column(name = "course_name", length = 100)
    private String courseName;

    @Column(name = "paper_id")
    private Long paperId;

    @Column(name = "paper_code", length = 50)
    private String paperCode;

    @Column(name = "paper_name", length = 200)
    private String paperName;

    @Column(name = "exam_date")
    private LocalDateTime examDate;

    @NotNull(message = "考试开始时间不能为空")
    @Column(name = "exam_start_time", nullable = false)
    private LocalDateTime examStartTime;

    @NotNull(message = "考试结束时间不能为空")
    @Column(name = "exam_end_time", nullable = false)
    private LocalDateTime examEndTime;

    @Column(name = "duration_minutes")
    private Integer durationMinutes; // 考试时长（分钟）

    @Column(name = "total_score")
    private BigDecimal totalScore;

    @Column(name = "passing_score")
    private BigDecimal passingScore;

    @Column(name = "max_attempts")
    @Builder.Default
    private Integer maxAttempts = 1;

    @Column(name = "attempt_count")
    @Builder.Default
    private Integer attemptCount = 0;

    @Column(name = "retake_allowed")
    @Builder.Default
    private Boolean retakeAllowed = false;

    @Column(name = "max_retakes")
    @Builder.Default
    private Integer maxRetakes = 0;

    @Column(name = "late_submission_allowed")
    @Builder.Default
    private Boolean lateSubmissionAllowed = false;

    @Column(name = "late_submission_penalty")
    private BigDecimal lateSubmissionPenalty;

    @Column(name = "randomize_questions")
    @Builder.Default
    private Boolean randomizeQuestions = false;

    @Column(name = "show_results_immediately")
    @Builder.Default
    private Boolean showResultsImmediately = false;

    @Column(name = "show_correct_answers")
    @Builder.Default
    private Boolean showCorrectAnswers = false;

    @Column(name = "allow_review")
    @Builder.Default
    private Boolean allowReview = true;

    @Column(name = "review_start_time")
    private LocalDateTime reviewStartTime;

    @Column(name = "review_end_time")
    private LocalDateTime reviewEndTime;

    @Enumerated(EnumType.STRING)
    @Column(name = "exam_mode", length = 20)
    @Builder.Default
    private ExamMode examMode = ExamMode.ONLINE;

    @Column(name = "location", length = 200)
    private String location;

    @Column(name = "instructions", columnDefinition = "TEXT")
    private String instructions;

    @Column(name = "proctoring_required")
    @Builder.Default
    private Boolean proctoringRequired = false;

    @Column(name = "proctoring_type", length = 30)
    private String proctoringType;

    @Column(name = "access_code", length = 50)
    private String accessCode;

    @Column(name = "password_required")
    @Builder.Default
    private Boolean passwordRequired = false;

    @Column(name = "exam_password", length = 100)
    private String examPassword;

    @Column(name = "ip_restriction")
    @Builder.Default
    private Boolean ipRestriction = false;

    @Column(name = "allowed_ips", length = 500)
    private String allowedIps;

    @Column(name = "browser_restrictions")
    @Builder.Default
    private Boolean browserRestrictions = false;

    @Column(name = "disable_right_click")
    @Builder.Default
    private Boolean disableRightClick = true;

    @Column(name = "disable_copy_paste")
    @Builder.Default
    private Boolean disableCopyPaste = true;

    @Column(name = "full_screen_required")
    @Builder.Default
    private Boolean fullScreenRequired = true;

    @Column(name = "time_warnings")
    @Builder.Default
    private Boolean timeWarnings = true;

    @Column(name = "auto_submit")
    @Builder.Default
    private Boolean autoSubmit = true;

    @Column(name = "grace_period_minutes")
    @Builder.Default
    private Integer gracePeriodMinutes = 5;

    @Column(name = "participant_count")
    @Builder.Default
    private Integer participantCount = 0;

    @Column(name = "completed_count")
    @Builder.Default
    private Integer completedCount = 0;

    @Column(name = "passed_count")
    @Builder.Default
    private Integer passedCount = 0;

    @Column(name = "failed_count")
    @Builder.Default
    private Integer failedCount = 0;

    @Column(name = "average_score")
    private BigDecimal averageScore;

    @Column(name = "highest_score")
    private BigDecimal highestScore;

    @Column(name = "lowest_score")
    private BigDecimal lowestScore;

    @Column(name = "pass_rate")
    private BigDecimal passRate;

    @Column(name = "is_template")
    @Builder.Default
    private Boolean isTemplate = false;

    @Column(name = "template_task_id")
    private Long templateTaskId;

    @Column(name = "is_published")
    @Builder.Default
    private Boolean isPublished = false;

    @Column(name = "published_time")
    private LocalDateTime publishedTime;

    @Column(name = "published_by", length = 50)
    private String publishedBy;

    @Column(name = "is_cancelled")
    @Builder.Default
    private Boolean isCancelled = false;

    @Column(name = "cancelled_time")
    private LocalDateTime cancelledTime;

    @Column(name = "cancelled_by", length = 50)
    private String cancelledBy;

    @Column(name = "cancellation_reason", length = 1000)
    private String cancellationReason;

    @Column(name = "is_completed")
    @Builder.Default
    private Boolean isCompleted = false;

    @Column(name = "completed_time")
    private LocalDateTime completedTime;

    @Column(name = "completion_notes", length = 1000)
    private String completionNotes;

    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    @CreatedDate
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @LastModifiedDate
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    // 关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "course_id", insertable = false, updatable = false)
    private com.edu.exam.entity.course.Course course;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "paper_id", insertable = false, updatable = false)
    private com.edu.exam.entity.exampaper.BasicExamPaper examPaper;

    @OneToMany(mappedBy = "assessmentTask", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private java.util.List<TaskAssignment> taskAssignments;

}